package com.kh.first.controller;

import com.kh.first.pojo.common.AjaxJson;
import com.kh.first.pojo.SettingPojo;
import com.kh.first.pojo.common.LayTableJson;
import com.kh.first.pojo.common.PageParam;

import com.kh.first.service.SettingService;
import com.kh.first.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("settingController")
public class SettingController {
    static final String currentFolder = UserUtils.THEME + "setting/";
    @Autowired
    private SettingService settingService;

    @RequestMapping(params = "goSettingList")
    public String goSettingList() {
    return currentFolder + "setting-list";
    }


    @RequestMapping(params = "dataGrid")
    @ResponseBody
    public LayTableJson<SettingPojo> dataGrid(SettingPojo pojo, PageParam pageParam) {
        LayTableJson<SettingPojo> ajaxJson = new LayTableJson<>();
        int limit = pageParam.getLimit();
        int page = pageParam.getPage();
        int size = limit == 0 ? 10 : limit;
        pojo.setStart(page == 0 ? 0 : (page - 1) * size);
        pojo.setSize(size);
        List<SettingPojo> SettingList = settingService.getSettingList(pojo);
        ajaxJson.data = SettingList;
        ajaxJson.count = settingService.countSettingList(pojo).intValue();
        return ajaxJson;
    }

    @RequestMapping(params = "goSettingAdd")
    public String goSettingAdd() {
        return currentFolder + "setting-add-update";
    }

    @RequestMapping(params = "goSettingQuery")
    public String goSettingQuery(Model model,SettingPojo pojo){
        model.addAttribute("setting", pojo);
        return currentFolder +"setting-query";
    }

    @RequestMapping(params = "goSettingUpdate")
    public String goSettingUpdate(SettingPojo setting, Model model) {
        List<SettingPojo> SettingList = settingService.getSettingList(setting);
        setting = SettingList.get(0);//报错让他报
        model.addAttribute("setting",setting);
        return currentFolder + "setting-add-update";
    }

    @RequestMapping(params = "doSettingAdd")
    @ResponseBody
    public AjaxJson<String> doUserAdd(SettingPojo setting) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        settingService.saveSetting(setting);
        return ajaxJson;
    }
    @RequestMapping(params = "doSettingUpdate")
    @ResponseBody
    public AjaxJson<String> doSettingUpdate(SettingPojo setting) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        settingService.updateSettingById(setting);
        return ajaxJson;
    }

}
